<?php
require_once __DIR__ . '/../autoload.php';

use  src\DataBase;

/**
 * 处理cms首页静态化业务逻辑
 * 有3种方案， 第一：定时(利用crontab来处理)  第二：人为触发 第三：在页面中控制时间来操作
 */
//header("content-type:text/htm;charset=utf-8");  300s

if (isset($_COOKIE['alreadyChoiceCourse'])) {
    header('location:./staticPage/alreadyChoiceCourse.html');
} else {
    if (is_file('./staticPage/room.html') && (time() - filemtime('./staticPage/room.html') < 0.1)) {    //触发静态页面的生成
        require_once('./staticPage/room.html');
    } else {
        if (isset($_COOKIE['room'])) {
            $roomID = explode(",", $_COOKIE['room']);
        } else {
            $db = new DataBase\DataBaseOperate('139.196.124.180', 'school', 'user1', 'Mcx857264', 'utf8');
            $selectResult = $db->select('room', '*');
            $result = $selectResult;
            $count = count($result);
            for ($i = 0; $i < $count - 1; $i++) {
                $roomResult .= $result[$i][1] . ',';
            }
            $roomResult .= $result[$count - 1][1];
            $expire = time() + 60;
            setcookie("room", $roomResult, $expire);
            $roomID = explode(",", $roomResult);
        }
        ob_start();
        require_once('./template/room.php');  //还是一个动态页面(localhost/staticpage/index.php)
        $s = ob_get_contents();
        file_put_contents('./staticPage/room.html', $s);

        //ob_clean();
    }
}